package com.echo.activity;

import android.content.Context;
import android.hardware.Camera;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import com.echo.util.DateUtil;
import com.echo.util.TimeUtil;

import net.bozho.easycamera.DefaultEasyCamera;
import net.bozho.easycamera.EasyCamera;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

public class CameraActivity extends AppCompatActivity {

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    private EasyCamera mEasyCamera;
    private EasyCamera.CameraActions mCameraActions;
    private boolean previewIsRunning = false;
    private Button mCapture;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        mCapture= (Button) findViewById(R.id.camera_capture);
        mSurfaceView= (SurfaceView) findViewById(R.id.camera_view);
        mSurfaceHolder=mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                //　开启后置摄像头
                mEasyCamera = DefaultEasyCamera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
                // 设置相机翻转角度
                //  mEasyCamera.setDisplayOrientation(90);
                //　正常显示相机预览
                WindowManager manager = (WindowManager) CameraActivity
                        .this.getSystemService(Context.WINDOW_SERVICE);
                mEasyCamera.alignCameraAndDisplayOrientation(manager);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                if(!previewIsRunning && mEasyCamera != null) {
                    try {
                        // 开启预览
                        mCameraActions =  mEasyCamera.startPreview(holder);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    previewIsRunning = true;
                }
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // 停止预览并释放
                mEasyCamera.stopPreview();
                mEasyCamera = null;
            }
        });
        mCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyCamera.PictureCallback mPictureCallback = new EasyCamera.PictureCallback(){

                    @Override
                    public void onPictureTaken(byte[] data, EasyCamera.CameraActions actions) {
                        // 停止预览
                        mEasyCamera.stopPreview();
                        try {
                            // 延时１０００ｓ，看的更加清楚
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        // 存储图片的操作
                        FileOutputStream fos = null;
                        try {
                            Log.d("TAG",getExternalFilesDir(Environment.DIRECTORY_PICTURES)+ File.separator+ TimeUtil.getTimeStr(Calendar.getInstance())+".jpg");
                            File pic=new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES+ File.separator),TimeUtil.getTimeStr(Calendar.getInstance())+".jpg");
                            fos = new FileOutputStream(pic);
                            fos.write(data);
                            fos.close();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        // 开启预览
                        EasyCamera.Callbacks.create().withRestartPreviewAfterCallbacks(true);
                        try {
                            mEasyCamera.startPreview(mSurfaceHolder);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                };
                // 拍摄照片的操作
                mCameraActions.takePicture(EasyCamera.Callbacks.create().withJpegCallback(mPictureCallback));
            }
        });
    }

}
